package com.exc.controller;

import com.exc.dao.ResumeDao;
import com.exc.pojo.Resume;
import com.exc.util.ControllerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Optional;

/**
 * @author Huang Min
 * @date 2020/8/27 19:34
 * @description
 */
@Controller
@RequestMapping("/r")
public class ResumeController {

    @Autowired
    private ResumeDao resumeDao;
    /*
    新增或修改
     */
    @PostMapping("/add")
    public String insert(String name,String address,String phone){
        // 新增和更新都使用save方法，通过传入的对象的主键有无来区分，没有主键信息那就是新增，有主键信息就是更新
        System.out.println("****新增****");
        Resume resume = new Resume(name,address,phone);

//        if(resume.getName() == null | resume.getName().equals("")){
//
//            return "redirect:toAdd";
//        }

        Resume resume1 = resumeDao.save(resume);
        System.out.println(resume1);

        return "redirect:list";
    }

    /*
 跳转到新增页面
  */
    @GetMapping("/toAdd")
    public ModelAndView toAdd(){
        System.out.println("****跳转到新增页面****");

        ModelAndView modeAndView =new ModelAndView();
        modeAndView.setViewName("add");
        return modeAndView;
    }

    /*
  新增或修改
   */
    @PostMapping("/update")
    public ModelAndView update(Long id,String name,String password,String phone){
        // 新增和更新都使用save方法，通过传入的对象的主键有无来区分，没有主键信息那就是新增，有主键信息就是更新
        Resume resume = new Resume(id,name,password,phone);
        System.out.println("****修改****");
        ModelAndView modelAndView = new ModelAndView();



        Resume resume1 = resumeDao.save(resume);
        System.out.println(resume1);

        System.out.println("返回修改页面");
        modelAndView.addObject(resume1);
        modelAndView.setViewName("update");
        return modelAndView;
    }


    /*
    删除
     */
    @GetMapping("/delete/{id}")
    public ModelAndView delete(@PathVariable("id")Long id){
        System.out.println("****删除****"+id);
        resumeDao.deleteById(id);

        List<Resume> list = resumeDao.findAll();
        for (int i = 0; i < list.size(); i++) {
            Resume resume =  list.get(i);
            System.out.println(resume);
        }
        ModelAndView modeAndView = ControllerUtil.createModeAndView(list);
        modeAndView.setViewName("list");
        return modeAndView;
    }



    /*
    查询单个
     */
    @RequestMapping(value = "/get/{id}" ,method = {RequestMethod.GET})
    public ModelAndView selectOne(@PathVariable("id")Long id){
        System.out.println("****查询单个****"+id);
        Optional<Resume> optional = resumeDao.findById(id);
        Resume resume1 = optional.get();
        System.out.println(resume1);
        ModelAndView modeAndView = ControllerUtil.createModeAndView(resume1);
        modeAndView.addObject("resume",resume1);
        modeAndView.setViewName("getOne");
        return modeAndView;
    }

    /*
    查询集合,列表查询
     */
    @GetMapping("/list")
    public ModelAndView selectList(){
        List<Resume> list = resumeDao.findAll();
        for (int i = 0; i < list.size(); i++) {
            Resume resume =  list.get(i);
            System.out.println(resume);
        }
        ModelAndView modeAndView = ControllerUtil.createModeAndView(list);
        modeAndView.setViewName("list");
        return modeAndView;

    }

}
